//
//  HHChangPhoneNumberViewController.m
//  HHBroker
//
//  Created by He Wei on 9/30/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHChangePhoneNumberViewController.h"
#import "HttpRequestManager.h"
#import "JiaYuanShareData.h"
#import "NSString+ValidateArgument.h"
#import "JiaYuanEncrypt.h"
#import "HHEnterPhoneNumberViewController.h"

@interface HHChangePhoneNumberViewController () <UITextFieldDelegate>

@property (nonatomic, weak) IBOutlet UITextField *textField;
@property (nonatomic, weak) IBOutlet UIButton *returnButton;
@end

@implementation HHChangePhoneNumberViewController

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"更换绑定手机", nil);
    self.returnButton.enabled = NO;
    self.hidesBottomBarWhenPushed = YES;

    
    
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField  resignFirstResponder];
    return YES;
}

- (IBAction)textFieldValueChanged:(UITextField *)sender
{
    if (_textField.text.length > 0 ) {
    
        self.returnButton.enabled = YES;
    }
    // self.returnButton.enabled = sender.text.length > 0 ? YES : NO;
}

#pragma mark - Event

- (IBAction)returnBurronClicked
{
    NSString *oldPwd = self.textField.text;
    
    if (![NSString validatePassword:oldPwd]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"密码长度必须8位及以上" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    // 测试
    HHEnterPhoneNumberViewController *enterPhone = [[HHEnterPhoneNumberViewController alloc] init];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"返回", nil) style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    [self.navigationController pushViewController:enterPhone animated:YES];
    return;
    //
    
    //加入加密字段
    oldPwd = [JiaYuanEncrypt AES128Encrypt:oldPwd key:kSecuCode];
    NSString *urlString = [NSString stringWithFormat:@"%@&password=%@", KUrlVerifyPwd, oldPwd];
    
     MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hub.mode = MBProgressHUDModeIndeterminate;
    [HttpRequestManager getWithUrlString:urlString success:^(id response) {
        
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        if (code == 1) {
           // hub.labelText = @"验证成功";
            hub.completionBlock = ^ {
                HHEnterPhoneNumberViewController *enterPhone = [[HHEnterPhoneNumberViewController alloc] init];
                UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"返回", nil) style:UIBarButtonItemStylePlain target:nil action:nil];
                self.navigationItem.backBarButtonItem = item;
                [self.navigationController pushViewController:enterPhone animated:YES];
            };
            [hub hide:YES afterDelay:1];
        } else {
            NSString *string  = [response objectForKey:@"msg"];
            hub.mode = MBProgressHUDModeText;
            hub.labelText = string;
            [hub hide:YES afterDelay:1];
        }
    
    } failure:^(NSError *error) {
        hub.mode = MBProgressHUDModeText;
        hub.labelText = @"网络有点问题";
        [hub hide:YES afterDelay:1];
    } isLogin:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];

}

@end
